package com.atguigu.gulimail.gateway.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
/**
 * @author 雷子
 */ // 跨域配置
@Configuration
public class GulimailCorsConfiguration {

    @Bean   // @bean表示把它加入到容器中
    public CorsWebFilter corsWebFilter(){
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

        // 创建一个跨域配置

        CorsConfiguration corsConfiguration = new CorsConfiguration();
        // 允许所有请求头能跨域
        corsConfiguration.addAllowedHeader("*");
        // 允许所有请求方法能跨域 （get put delete）
        corsConfiguration.addAllowedMethod("*");
        // 允许所有请求来源能跨域
        corsConfiguration.addAllowedOriginPattern("*");
        // 允许携带cookie进行跨域
        corsConfiguration.setAllowCredentials(true);
        // 添加跨域的配置
        source.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsWebFilter( source);
    }

}
